﻿using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public float moveSpeedHorizontal = 0.5f;
    public float moveSpeedVertical = 0.5f;
    public GameObject bullets;
    public float bulletSpeed = 2;

    // Use this for initialization
    void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        float h = Input.GetAxis("Horizontal");
        float w = Input.GetAxis("Vertical");
        transform.Translate(Vector3.right * h * moveSpeedHorizontal);
        transform.Translate(Vector3.up * w * moveSpeedVertical);
        //bullets = GameObject.Find("Bullets"); 不建议每一帧都使用
        for (int i = 0; i < bullets.transform.childCount; i++)
        {
            GameObject bullet = bullets.transform.GetChild(i).gameObject;
            bullet.transform.Translate(Vector3.up * bulletSpeed * Time.deltaTime);
            if (bullet.transform.position.y - transform.position.y > 13)
            {
                Destroy(bullet);
            }
        }
    }

    void OnMouseDrag()
    {
        Vector3 position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position = new Vector3(position.x,position.y,0);
    }
}
